
#include "Question.h"

Question * creerQuestion()
{
    Question * Q = (Question*)malloc(1 * sizeof(Question));
    initQuestion(Q);
    return Q;
}

void initQuestion(Question * CC)
{
	int i;
	CC->Quest = (char*) malloc (64 * sizeof (char));
	CC->tabRep = (Reponse*)malloc(4 * sizeof(Reponse));
	for ( i=0 ; i<4 ; i++ )
	{
		initReponse(&CC->tabRep[i]);
	}
}

void initReponse(Reponse * Rep)
{
	Rep->rep = (char*) malloc (64 * sizeof (char));
    Rep->VF = vide;
}

void setQuest(Question * CC, const char * texte)
{
	strcpy(CC->Quest, texte);
}

void setRep(Reponse * Rep, const char * texte, const boolRep vrfx)
{
	strcpy(Rep->rep, texte);
	Rep->VF = vrfx;
}


void setRepQuest(Question * Q, const char * texte)
{
    int i;
    int verif = 0;
    for(i=0;i<4;i++)
    {
        if(Q->tabRep[i].VF == 2 && verif == 0)
        {
            if(i == 0)
            {
                setRep(&Q->tabRep[i], texte,1);
            }
            else
            {
                setRep(&Q->tabRep[i], texte,0);
            }
            verif = 1;
        }

    }
}

char * getQuest(const Question * CC)
{
	return CC->Quest;
}

char * getRep(const Reponse * Rep)
{
	return Rep->rep;
}

char * getRepJuste(const Question * CC)
{
	return getRep(&CC->tabRep[0]);
}

boolRep getVF(const Reponse * Rep)
{
	return Rep->VF;
}

void libereQuest(Question * CC)
{
    int i;
    for ( i=0 ; i<4 ; i++ )
	{
		libereRep(&CC->tabRep[i]);
	}
	free(CC->Quest);
	free(CC->tabRep);
}

void detruireQuest(Question ** CC)
{
    libereQuest(*CC);
    free(*CC);
    *CC = NULL;
}

void libereRep(Reponse * Rep)
{
    free(Rep->rep);
	Rep->VF = vide;
}

void copierQuest(Question * Q1, const Question * Q2)
{
    int i;
    setQuest(Q1, Q2->Quest);
    for(i=0;i<4;i++)
    {
	strcpy(Q1->tabRep[i].rep, Q2->tabRep[i].rep);
        Q1->tabRep[i].VF = Q2->tabRep[i].VF;
    }
}

int nbrAleatoire(const int nbr)
{
	int i;
	i = rand()%nbr;
	return i;
}

void repAlea(const Question * quest, char * rep[4])
{
	int i, nbr;
	nbr = nbrAleatoire(4);
	for(i=0;i<4;i++)
	{
		rep[i] = getRep(&quest->tabRep[nbr]);
		nbr++;
		if(nbr == 4)
			nbr = 0;
	}
}

void questionTestRegression()
{
    int i;
    char * texte = "Quel est ton age mon amis ?";
    char * texte1 = "Mon age est 17";
    char * texte2 = "Mon age est 3!!";

    Reponse pRep;
    Question * pQuest = creerQuestion();
    Question * Q2 = creerQuestion();
    initReponse(&pRep);

    printf("** Test De Regression : Question **\n");
    assert(pRep.VF == vide);

    for(i=0;i<4;i++)
    {
        assert(pQuest->tabRep[i].VF == 2);
    }


    printf("* Appel a setQuest...");
    setQuest(pQuest,texte);
    printf(" okay *\n");
    printf("* Appel a setRepQuest...");
    setRepQuest(pQuest,texte1);
    setRepQuest(pQuest,texte2);
	setRepQuest(pQuest,texte1);
    setRepQuest(pQuest,texte2);
    printf(" okay *\n");
    copierQuest(Q2,pQuest);
	/** On affiche la réponse vraie*/
    for(i=0;i<4;i++)
    {
        if(Q2->tabRep[i].VF == 0)
        {
            printf("* La question est : %s *\n* La réponse %d avec bool = %d : %s *\n",getQuest(Q2),i,getVF(&Q2->tabRep[i]),getRep(&Q2->tabRep[i]));
        }
    }

    libereRep(&pRep);
    detruireQuest(&pQuest);
    detruireQuest(&Q2);
    printf("* Fin Test De Regression: OK *\n\n");
}

